package com.example.bodyguard;

import android.content.Context;
import android.util.Log;

import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTNotificationMessage;
import com.igexin.sdk.message.GTTransmitMessage;

import org.greenrobot.eventbus.EventBus;

/**
 * 继承 GTIntentService 接收来自个推的消息, 所有消息在线程中回调, 如果注册了该服务, 则务必要在 AndroidManifest中声明, 否则无法接受消息<br>
 * onReceiveMessageData 处理透传消息<br>
 * onReceiveClientId 接收 cid <br>
 * onReceiveOnlineState cid 离线上线通知 <br>
 * onReceiveCommandResult 各种事件处理回执 <br>
 */
public class DemoIntentService extends GTIntentService {

    public DemoIntentService() {

    }

    @Override
    public void onReceiveServicePid(Context context, int pid) {
    }

    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage msg) {
        Log.e(TAG, "onReceiveMessageData -> " + "msg.getPayload = " + msg.getPayload());
        Log.e(TAG, "onReceiveMessageData -> " + "msg.getMessageId = " + msg.getMessageId());
        Log.e(TAG, "onReceiveMessageData -> " + "msg.getPayloadId = " + msg.getPayloadId());
        Log.e(TAG, "onReceiveMessageData -> " + "msg.getTaskId = " + msg.getTaskId());

        String s = changeString(msg.getPayload());
        Log.e(TAG, "changeString -> " + s);

//        {time=Fri Apr 13 15:38:48 GMT+08:00 2018, type=监听到了异响}

//        time,Fri Apr 13 15:38:48 GMT+08:00 2018, type,监听到了异响

        PushBean pushBean = new PushBean();
        s = s.replace("{", "=");
        s = s.replace("}", "=");
        s = s.replace("=", ",");
        String[] strs = s.split(",");
        if (strs != null && strs.length > 0) {
            pushBean.time = strs[2];
            pushBean.type = strs[4];
        }

        Log.e("buxq", "onReceiveMessageData: " + pushBean.toString());
        EventBus.getDefault().post(pushBean);
    }


    public String changeString(byte[] payload) {
        return new String(payload);
    }


    @Override
    public void onReceiveClientId(Context context, String clientid) {
        Log.e(TAG, "onReceiveClientId -> " + "clientid = " + clientid);
    }

    @Override
    public void onReceiveOnlineState(Context context, boolean online) {
    }

    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage cmdMessage) {
    }

    @Override
    public void onNotificationMessageArrived(Context context, GTNotificationMessage gtNotificationMessage) {

    }

    @Override
    public void onNotificationMessageClicked(Context context, GTNotificationMessage msg) {
    }
}